import {
  BrowserRouter as Router,
  Route,
  Link,
  Switch,
  useHistory,
} from "react-router-dom";

const Home = () => {
  // router的所有的组件和API也好需要在Router包裹的内容组件下使用
  const history = useHistory();
  const onClick = () => {
    // 跳转页面一
    history.replace("/first");
  };
  return (
    <div>
      Home组件 <button onClick={onClick}>页面1</button>
    </div>
  );
};
const First = () => {
  return <div>First组件</div>;
};

const App = () => {
  return (
    <Router>
      <div className="app">
        <h1>根组件</h1>
      </div>
      <ul>
        <li>
          <Link to="/">首页</Link>
        </li>
        <li>
          {/* <Link to="/first">页面一</Link> */}
          <Link to="/first">页面一</Link>
        </li>
      </ul>
      {/* 路由规则 */}
      {/* 让Route组件被匹配的第一个组件渲染，其他不渲染 */}
      <Switch>
        <Route path="/" exact component={Home}></Route>
        <Route path="/first" component={First}></Route>
        <Route>404页面</Route>
      </Switch>
    </Router>
  );
};

export default App;
